class Solution {
public:
    int maxProfit(vector<int> &prices) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (prices.size() == 0) {
            return 0;
        }
        int ans = 0;
        int minValue = prices[0];
        int n = prices.size();

        for (int i = 1; i < n; ++i) {
            if (prices[i] - minValue > ans) {
                ans = prices[i] - minValue;
            }
            minValue = min(minValue, prices[i]);
        }
        return ans;
    }
};
